package gfo

class Match {
    int id
    Bracket bracket
    Stage stage
    Tournament tournament
    String vidURL
    static hasMany = [players: Matchship]

    static constraints = {
        stage(nullable: true)
        vidURL(nullable: true)
    }

    String toString(){
        def msList = players.toList()
        def winner = null
        def loser = null
        for (ms in msList){
            if (ms.winner){
                winner = ms.player
            }
            else{
                loser = ms.player
            }
        }
        "${winner?.fullName} v ${loser?.fullName}, $bracket, ${tournament.toAbbrv()}"
    }

    /*getWinner
    *----------------------
    * Searches through the matchships to find the winner of the matches (there should be one
    * and only one. Otherwise returns null.*/
    Player determineWinner(){
        for (ms in players) {
            if (ms.winner) {
                return ms.player
            }
        }
        return null
    }
}
